import { createAction, actionTypeCreatorWithNameSpace } from './util';
import agent from 'agent';
import {getEnvironmentId} from './environment';

const actionTypeCreator = actionTypeCreatorWithNameSpace('service');

export const fetchServiceBySearch = createAction(actionTypeCreator('serviceList'), (query) => {
  return agent.get(`/api/v1/environments/${getEnvironmentId()}/services`)
    .query({ query })
    .then(response => response.body);
}, {
  fetch: {
    processing: true
  }
});

export const clearServiceBySearch = createAction(actionTypeCreator('serviceList'), () => {
  return [];
});

export const fetchServiceListByMachine = createAction(actionTypeCreator('serviceListByMachine'), (machine) => {
  return agent.get(`/api/v1/environments/${getEnvironmentId()}/services`)
    .query({ machine })
    .then(response => response.body);
});

export const fetchServiceListByApplication = createAction(actionTypeCreator('serviceListByApplication'), (application) => {
  return agent.get(`/api/v1/environments/${getEnvironmentId()}/services`)
    .query({ application })
    .then(response => response.body);
});

export const getServiceById = createAction(actionTypeCreator('serviceById'), (serviceId) => {
  return agent.get(`/api/v1/environments/${getEnvironmentId()}/services/${serviceId}`)
    .then(response => response.body);
});

export const updateServiceTag = createAction(actionTypeCreator('serviceById'), (serviceId, tags) => {
  return agent.post(`/api/v1/environments/${getEnvironmentId()}/services/${serviceId}/tags`)
    .type('form')
    .send({ tags })
    .then(response => response.body);
});

export const fetchProviderListByService = createAction(actionTypeCreator('providerListByService'), (serviceId) => {
  return agent.get(`/api/v1/environments/${getEnvironmentId()}/services/${serviceId}/providers`)
    .then(response => response.body);
});

export const fetchConsumerListByService = createAction(actionTypeCreator('consumerListByService'), (serviceId) => {
  return agent.get(`/api/v1/environments/${getEnvironmentId()}/services/${serviceId}/consumers`)
    .then(response => response.body);
});

export const fetchTracingListByService = createAction(actionTypeCreator('tracingListByService'), (serviceId) => {
  return agent.get(`/api/v1/environments/${getEnvironmentId()}/services/${serviceId}/tracings`)
    .then(response => response.body);
});

export const fetchTracingDetailList = createAction(actionTypeCreator('tracingDetailList'), (serviceId, rootId) => {
  return agent.get(`/api/v1/environments/${getEnvironmentId()}/services/${serviceId}/tracings/${rootId}`)
    .then(response => response.body);
});

export const enableTracing = createAction(actionTypeCreator('enableTracing'), (serviceId, times) => {
  return agent.post(`/api/v1/environments/${getEnvironmentId()}/services/${serviceId}/tracings`)
    .type('form')
    .send({ tracingTimes: times })
    .then(response => response.body);
});

export const deleteTracingRecord = createAction(actionTypeCreator('tracingDetailList'), (serviceId, tracingIds) => {
  return agent.del(`/api/v1/environments/${getEnvironmentId()}/services/${serviceId}/tracings`)
    .send(tracingIds);
});

export const updatePermission = createAction(actionTypeCreator('serviceById'), (serviceId, allow, denial) => {
  return agent.post(`/api/v1/environments/${getEnvironmentId()}/services/${serviceId}/permissions`)
    .type('form')
    .send({
      allow: allow,
      denial: denial
    })
    .then(response => response.body);
});
